001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.data;
025    
026    import EVolve.*;
027    import EVolve.util.HelperFuncs;
028    
029    import java.util.*;
030    
031    public class ReferenceLink implements Cloneable{
032        private String name; // name of the link
033        private int sourceType; // type of the source element
034        private int sourceIndex; // field index of the source element
035        private int targetType; // type of the target entity
036        private HashMap target; // id of the targets
037        private String description; // description of the link
038        private HashSet properties;
039    
040        /**
041         * Creates a reference link.
042         *
043         * @param  name  name of the link
044         * @param  sourceType  type of the source element
045         * @param  sourceIndex  field index of the source element
046         * @param  targetType  type of the target entity
047         * @param  description  description of the link
048         * @param  properties properties of the link
049         */
050        public ReferenceLink(String name, int sourceType, int sourceIndex, int targetType, String description, String[] properties) {
051            this.name = name;
052            this.sourceType = sourceType;
053            this.sourceIndex = sourceIndex;
054            this.targetType = targetType;
055            this.description = description;
056            this.properties = new HashSet();
057            for (int i=0; i<properties.length; i++)
058                this.properties.add(properties[i]);
059            if (properties.length == 0)
060                this.properties.add("reference");
061    
062            target = Scene.getDataManager().getEntity()[targetType];
063            /*target = new int[Scene.getDataManager().getEntity()[targetType].length];
064            for (int i = 0; i < target.length; i++) {
065                target[i] = i;
066            }*/
067        }
068    
069        /**
070         * Creates a reference link by combining two reference links.
071         *
072         * @param  from  where the link starts
073         * @param  to  where the link ends
074         */
075        public ReferenceLink(ReferenceLink from, ReferenceLink to) {
076            this.name = to.name;
077            this.sourceType = from.sourceType;
078            this.sourceIndex = from.sourceIndex;
079            this.targetType = to.targetType;
080            this.description = from.description + "  >>>  " + to.description;
081    
082            /*target = new int[from.target.length];
083            for (int i = 0; i < target.length; i++) {
084                target[i] = to.target[Scene.getDataManager().getEntity()[from.targetType][from.target[i]].getField()[to.sourceIndex]];
085            }*/
086            target = new HashMap();
087            Iterator it = from.target.keySet().iterator();
088            while (it.hasNext()) {
089                Object key = it.next();
090                Object fromKey = new Long(((Entity)from.target.get(key)).getField()[to.sourceIndex]);
091                Entity entity = (Entity)Scene.getDataManager().getEntity()[from.targetType].get(fromKey);
092                target.put(key,entity);
093            }
094            this.properties = HelperFuncs.cloneHashSet(from.properties);
095        }
096    
097        /**
098         * Gets the name of the link.
099         *
100         * @return  name of the link
101         */
102        public String getName() {
103            return name;
104        }
105    
106        /**
107         * Gets the type of the source element.
108         *
109         * @return  type of the source element
110         */
111        public int getSourceType() {
112            return sourceType;
113        }
114    
115        /**
116         * Gets the field index of the source element.
117         *
118         * @return  field index of the source element
119         */
120        public int getSourceIndex() {
121            return sourceIndex;
122        }
123    
124        /**
125         * Gets the type of the target entity.
126         *
127         * @return  type of the target entity
128         */
129        public int getTargetType() {
130            return targetType;
131        }
132    
133        /**
134         * Gets the target.
135         *
136         * @param  source reference
137         * @return  target
138         */
139        public long getTarget(long source) {
140            return ((Entity)target.get(new Long(source))).getId();
141        }
142    
143        /**
144         * Gets the description of the link.
145         *
146         * @return  description of the link
147         */
148        public String getDescription() {
149            return description;
150        }
151    
152        public boolean hasProperty(String property) {
153            return properties.contains(property);
154        }
155    
156        public Object clone() {
157            ReferenceLink o = null;
158            try {
159                o = (ReferenceLink)super.clone();
160            } catch (CloneNotSupportedException e) {
161                e.printStackTrace();
162                return null;
163            }
164            o.name = name;
165            o.target = HelperFuncs.cloneHashMap(target) ;
166            return o;
167        }
168    }